package com.yx.cloud.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.iot.base.service.req.FeedBackParam;
import com.iot.sweeper.AppContext;
import com.yx.cloud.Interface.BounceCallBack;
import com.yx.cloud.Interface.EventForwardingHelper;
import com.yx.cloud.Interface.OnItemClickListener;
import com.yx.cloud.R;
import com.yx.cloud.adapter.FeedBackMesListAdapter;
import com.yx.cloud.base.BaseActivity;
import com.iot.base.bean.account.UserFeedBack;
import com.yx.cloud.databinding.FeedbackmeslsitActivityBinding;
import com.yx.cloud.header.NormalBounceHandler;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.yx.cloud.view.DefaultHeader;
import com.iot.common.utils.Event;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class FeedBackMesListActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "FeedBackMesLsitActivity";

    private List<UserFeedBack> mFeedbackTalkBeans;
    private FeedbackmeslsitActivityBinding binding;

    @Override
    public void onCreate() {
        binding = FeedbackmeslsitActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initData();
    }

    private FeedBackMesListAdapter adapter;
    private List<UserFeedBack> feedbackTalkBeans;

    private void initData() {

        binding.lottieAnimationView.setVisibility(View.VISIBLE);
        binding.bl.setBounceHandler(new NormalBounceHandler(), binding.feedbackListView);//设置滚动冲突的控制类
        binding.bl.setEventForwardingHelper(new EventForwardingHelper() {//自定义事件分发处理
            @Override
            public boolean notForwarding(float downX, float downY, float moveX, float moveY) {
                return true;
            }
        });
        binding.bl.setHeaderView(new DefaultHeader(this), binding.flRoot);//设置刷新头，null意味着没有刷新头，不调用该函数意为着空
//        bounceLayout.setFooterView(new DefaultFooter(this), rootView);//设置底部刷新
        mFeedbackTalkBeans = new ArrayList<>();

        //线性布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.feedbackListView.setLayoutManager(linearLayoutManager);

        adapter = new FeedBackMesListAdapter(FeedBackMesListActivity.this, mFeedbackTalkBeans);
        binding.feedbackListView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent feedBackDetails = new Intent(FeedBackMesListActivity.this, FeedbackDetailsActivity.class);
                feedBackDetails.putExtra("hdid", feedbackTalkBeans.get(position).getId());
                feedBackDetails.putExtra("hdType", feedbackTalkBeans.get(position).getType());
                startActivity(feedBackDetails);
            }
        });
        // 设置上拉加载更多
        binding.bl.setBounceCallBack(new BounceCallBack() {
            @Override
            public void startRefresh() {
                //下拉
                //查询反馈会话列表
                AppContext.getAccountService().getFeedback(FeedBackParam.newBuilder()
                        .type(FeedBackParam.GET_TYPE_BY_USER)
                        .userId(AppContext.get().getAccount().getId())
                        .build());
            }

            @Override
            public void startLoadingMore() {
                //上拉
                //查询反馈会话列表
                AppContext.getAccountService().getFeedback(FeedBackParam.newBuilder()
                        .type(FeedBackParam.GET_TYPE_BY_USER)
                        .userId(AppContext.get().getAccount().getId())
                        .build());
            }
        });
        binding.ivFeedBackMesLsitReturn.setOnClickListener(this);



    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_feedBackMesLsitReturn) {
            Intent_utils.backIntent(this);
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_FEEDBACK_LIST:
                binding.lottieAnimationView.setVisibility(View.GONE);
                //反馈信息列表
                // 加载完数据设置为不加载状态，将加载进度收起来
//                bounceLayout.setLoadingMoreCompleted();//底部收起
                binding.bl.setRefreshCompleted();//头部收起
                if (event.isResult()) {
                    feedbackTalkBeans = (List<UserFeedBack>) event.getContent();
                    Log.i(TAG, "查询反馈会话列表 : " + new Gson().toJson(feedbackTalkBeans));
                    if (feedbackTalkBeans.size() > 0) {
                        binding.feedbackListView.setVisibility(View.VISIBLE);
                        binding.rlNoFeedBackMsg.setVisibility(View.GONE);
                    } else {
                        binding.feedbackListView.setVisibility(View.GONE);
                        binding.rlNoFeedBackMsg.setVisibility(View.VISIBLE);
                    }
                    mFeedbackTalkBeans.clear();
                    mFeedbackTalkBeans.addAll(feedbackTalkBeans);
                    adapter.notifyDataSetChanged();
                } else {
                    binding.feedbackListView.setVisibility(View.GONE);
                    binding.rlNoFeedBackMsg.setVisibility(View.VISIBLE);
                    ToastUtil.show(FeedBackMesListActivity.this, event.getContent() + "", Toast.LENGTH_SHORT);
                }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        AppContext.getAccountService().getFeedback(FeedBackParam.newBuilder()
                .type(FeedBackParam.GET_TYPE_BY_USER)
                .userId(AppContext.get().getAccount().getId())
                .build());
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}

